package com.faithbj.device.web.controller.admin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.faithbj.device.model.entity.Consumable;
import com.faithbj.device.model.entity.StockItem;
import com.faithbj.device.service.ConsumableService;
import com.faithbj.device.utils.DateUtils;

@Controller
@RequestMapping("/faith/consumable/")
public class AdminConsumableController extends BaseAdminController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4784506615400387088L;
	
	
   @Override
    public void initBinder(WebDataBinder binder) {  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
        dateFormat.setLenient(false);  
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, false));  
    }  
	
	private final String OPERRATE_RETURN_URL="list";
	private final String LIST="admin/consumable_info_list";
	private final String INPUT="admin/consumable_info_input";
	
	@Resource
	private ConsumableService consumableService;
	
	@RequestMapping("menu")
	public String index(){
		return "admin/menu_consumable";
	}
	
	// 列表
		@RequestMapping("list")
		public String list(ModelMap map) {
			pager = consumableService.findByPager(pager);
			map.put("pager", pager);
			return LIST;
		}
		
		//库存信息 列表
		@RequestMapping("detaillist")
		public String detailList(ModelMap map) {
			pager = consumableService.findByPager(pager);
			map.put("pager", pager);
			return "admin/consumable_stock_info_list";
		}

		// 添加
		@RequestMapping("new")
		public String add(ModelMap map) {
			map.put("isAddAction", true);
			return INPUT;
		}
		
		// 编辑
		@RequestMapping("/edit/{id}")
		public String edit(@PathVariable String id,ModelMap map) {
			Consumable con = consumableService.load(id);
			map.put("consumable", con);
			map.put("isAddAction", false);
			return INPUT;
		}
		
		// 删除
		@RequestMapping("delete/{id}")
		public String delete(@PathVariable String id,ModelMap map){
			Consumable con = consumableService.load(id);
			Set<StockItem> stockItemSet = con.getStockItemSet();
			if(stockItemSet.size() >0){
				map.put("errorMessages", "存在改耗材的出入库信息，不能删除");
				return ERROR;
			}
			consumableService.delete(id);
			map.put("redirectUrl", "../"+OPERRATE_RETURN_URL);
			return SUCCESS;
		}

		// 保存
		@RequestMapping(value="save",method = RequestMethod.POST)
		public String save(Consumable con, ModelMap map) {
			Consumable cons = consumableService.findByCode(con.getSn());
			if(cons!=null){
				map.put("errorMessages", "编号已经存在");
				return ERROR;
			}
			
			consumableService.save(con);
			map.put("redirectUrl", OPERRATE_RETURN_URL);
			return SUCCESS;
		}

		// 更新
		@RequestMapping(value="update",method = RequestMethod.POST)
		public String update(Consumable con, ModelMap map) {
			Consumable cons = consumableService.findByCode(con.getSn());
			Consumable persistent = consumableService.get(con.getId());
			if(cons!=null && !persistent.getSn().equals(con.getSn())){
				map.put("errorMessages", "编号已经存在");
				return ERROR;
			}
			
			BeanUtils.copyProperties(con, persistent, new String[] {"id", "createDate", "modifyDate","commonts","limityears","name","place","position","price","sn","unit","standard"});
//			persistent.setCommonts(con.getCommonts());
//			persistent.setDate(DateUtils.getStringToDate(sdate, "yyyy-MM-dd"));
//			persistent.setLimityears(con.getLimityears());
//			persistent.setName(con.getName());
//			persistent.setPlace(con.getPlace());
//			persistent.setPosition(con.getPosition());
//			persistent.setPrice(con.getPrice());
//			persistent.setSn(con.getSn());
//			persistent.setUnit(con.getUnit());
//			persistent.setStandard(con.getStandard());
			consumableService.update(persistent);
//			flushCache();
			map.put("redirectUrl", OPERRATE_RETURN_URL);
			return SUCCESS;
		}

}
